import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
//import java.util.Scanner;


public class ClientListeningThread implements Runnable {

private Socket socket;
//Scanner scanner;
ObjectInputStream in;
ObjectOutputStream out;
static QuadTree message = new QuadTree();
	
	
ClientListeningThread(Socket socket)
{
	this.socket = socket;
	in = null;
	out = null;
}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while (true) 
		{
			
			try 
			{
				//if (out == null) 
				//{
				//	out = new ObjectOutputStream(socket.getOutputStream());
				//}
				//read a string
				//System.out.println("Enter a message:");
				//String str = scanner.next();
				//send it to server
				//out.writeObject(new Message(111));
				//out.flush();

				//get the reply from the server
				//get data objects from the server
				// in the future will need to differentiate between different objects
				// for now we will only be updating the QuadTree variable via messages
				if (in == null) 
				{
					in = new ObjectInputStream(socket.getInputStream());
				}
				message = (QuadTree) in.readObject();
				InputThread.localTree = message;
				System.out.println("Server said: new quad tree with " + message.getNumberOfObjects() + " objects.");
				
			}
			catch(Exception e)
			{
				System.out.println("Error : " + e.getMessage());
				//serverSocket.close();
				 try {
					socket.close();
					//scanner.close();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
